<?php
/**
 * Created by PhpStorm.
 * User: 国小果
 * Date: 2022/12/6
 * Time: 13:33
 */

namespace app\common\lib;

include_once __DIR__ . '/../../../vendor/topthink/framework/src/think/Log.php';

class Log extends \think\log
{
    private static $sig = 0;

    public function log($level, $message, array $context = []): void
    {
        $message = self::getMessage($message);
        $time = date("Y-m-d H:i:s").'.'.explode('.', microtime(true))[1];
        parent::log($level, $time.' '.$message, $context);
    }

    private function getMessage($message){
        $message = self::getSig().$message;
        if(strpos($message,'start')){
            $message .=' Body=>'.json_encode(request()->param());
        }

        return $message;
    }

    private function getSig(){
        if(empty(self::$sig)) {
            self::$sig = '[' .dechex(rand(10000000, 99999999)).dechex(rand(10000000, 99999999)). '] ' . ' URL=>' . request()->baseUrl() . ' ';
        }
        return self::$sig;
    }
}